package com.haogu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.Leads;
import com.haogu.entity.PLeads;
import com.haogu.entity.VLeads;
import com.haogu.mapper.LeadsMapper;
import com.haogu.service.LeadsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LeadsServiceImpl implements LeadsService {
    @Resource
    private LeadsMapper leadsMapper;

    @Override
    public List<Leads> selectAll() {
        List<Leads> leads = leadsMapper.selectList(null);
        return leads;
    }

    @Override
    public List<Leads> selectLeadsIsDrawNoTranformd() {
        QueryWrapper<Leads> qw = new QueryWrapper<>();
        qw.isNotNull("owner_user_id");
        qw.eq("is_transformed",0);
        List<Leads> leads = leadsMapper.selectList(qw);
        return leads;
    }

    public boolean insertLeads(Leads leads){
        int insert = leadsMapper.insert(leads);
        return insert == 1 ? true : false;

    }

    public List<VLeads> searchLeads(PLeads p){
        List<VLeads> vLeads = leadsMapper.searchLeads((p.getPage()-1)*p.getRows(),p.getRows(),p);

         //今日创建 ，本周创建， 本月创建
        long len = 0;
        if(p.getType()!=null){
            switch(p.getType()){
                case "day":
                    len = 60*60*24;
                    break;
                case "week":
                    len = 60*60*24*7;
                    break;
                case "month":
                    len = 60*60*24*7*30;
                    break;
            }
        }
        if(len != 0){
            Date date = new Date();
            long time1 = date.getTime()/1000;
            ArrayList temp = new ArrayList();
            for(int i = 0; i < vLeads.size();i++){
                long time2 = vLeads.get(i).getCreateTime().getTime()/1000;
                if((time1 - time2)>len){
                  temp.add(i);
                }
            }
            for(int j = 0; j < temp.size(); j++){
                vLeads.remove(j);
            }
        }
        return vLeads;
    }

    @Override
    public List<VLeads> searchLeadsChi(PLeads p) {
        List<VLeads> vLeads = leadsMapper.searchLeadsChi((p.getPage()-1)*p.getRows(),p.getRows(),p);
        return vLeads;
    }

    public int rows(){
        Integer integer = leadsMapper.selectCount(null);
        return integer;
    }
    public Leads selectLeadsById(Integer id){
        Leads leads = leadsMapper.selectById(id);
        return leads;
    }

    @Override
    public boolean deleteLeadsById(Integer id) {
    
        int i = leadsMapper.deleteById(id);
        if(i!=0){
            updateDeleteTime(new Date(), id);
        }
        return i==0?false:true;
    }

    @Override
    public boolean updateLeadsById(Leads leads) {
        int i = leadsMapper.updateById(leads);
        return i==0?false:true;
    }

    @Override
    public boolean updateTransformedInfoById(Integer id, Integer userId, Integer contactsId, Integer customerId) {
        int i = leadsMapper.updateTransformedInfoById(id, userId, contactsId, customerId);
        return i==0?false:true;
    }

    @Override
    public boolean updateOwnerUserId(Integer leadsId, Integer userId) {
        int i = leadsMapper.updateOwnerUserId(leadsId, userId,new Date());
        return i==0?false:true;
    }

    @Override
    public boolean updateDeleteTime(Date date,Integer id) {
        int i = leadsMapper.updateDeleteTime(date, id);
        return i==0?false:true;
    }

}
